package com.controller.server;

import com.dao.FlightDaoImpl;
import com.dao.TransactionDaoImpl;
import com.dao.VacancyDaoImpl;
import com.util.Constant;
import com.util.Printer;
import corba.airlineServer.Flight;
import corba.airlineServer._ServerImplBase;

import java.util.List;

/**
 * Created by Phillip on 2014/4/22.
 */
public class CORBAServant extends _ServerImplBase {

    FlightDaoImpl flightDaoImpl = new FlightDaoImpl();
    TransactionDaoImpl transactionDaoImpl = new TransactionDaoImpl();
    VacancyDaoImpl vacancyDaoImpl = new VacancyDaoImpl();

    @Override
    public String[] queryCity2() {

        List<String> cities = flightDaoImpl.queryCityList(2);
        Printer.p("city size: " + cities.size());
        String[] cityStrings = new String[cities.size()];
        for (int i = 0; i < cities.size(); i++) {
            cityStrings[i] = cities.get(i);
        }
        Printer.p("return city size: " + cityStrings.length);
        return cityStrings;
    }

    @Override
    public Flight[] queryFlights2(String deptString, String destString, String deptDateString, String returnDateString) {
        List<com.dao.Flight> flights1 = flightDaoImpl.queryByCityDate(deptString, destString, deptDateString, 2);
        List<com.dao.Flight> flights2 = flightDaoImpl.queryByCityDate(destString, deptString, returnDateString, 2);
        if (flights1 == null || flights2 == null) {
            Printer.p("1 or 2 flights have no vacancy");
            return null;
        }
        flights1.addAll(flights2);
        Flight[] temp = new Flight[flights1.size()];
        for (int i = 0; i < flights1.size(); i++) {
            temp[i] = flights1.get(i).transfer();
        }
        return temp;
    }

    @Override
    public String book2(String flightNoString1, String deptString, String classString1, String flightNoString2,
                       String returnString, String classString2, String nameString, String creditString) {

        String transactionID1 = creditString + nameString + flightNoString1 + deptString;
        String transactionID2 = creditString + nameString + flightNoString2 + returnString;
        boolean b1 = transactionDaoImpl.queryTransaction(transactionID1, 2);
        boolean b2 = transactionDaoImpl.queryTransaction(transactionID2, 2);

        if (b1 && b2) {

            transactionDaoImpl.insertTransaction(transactionID1, flightNoString1, deptString, nameString, 2);
            transactionDaoImpl.insertTransaction(transactionID2, flightNoString2, deptString, nameString, 2);

            return vacancyDaoImpl.bookByFlightDateClass(flightNoString1, deptString, classString1, flightNoString2,
                    returnString, classString2, 2);

        } else if (!b1 || !b2) {
            Printer.p("this user has booked a ticket for the same flight before");
            return Constant.DUPLICATETICKETS_STRING;
        }
        return Constant.BOOKFAILED_STRING;

    }

    @Override
    public String queryRates2(String flightString, String classString) {
        return flightDaoImpl.queryRates(flightString, classString, 2);
    }

    @Override
    public String queryAirline2(String deptString, String dsctString) {
        if (flightDaoImpl.isSupported(deptString, dsctString, 2)) {

            return Constant.IS_SUPPORTED_STRING + "Qantas";
        } else {

            return Constant.NOTSUPPORTED_STRING + "Qantas";

        }
    }
}
